<?php

namespace App\Http\Controllers\Front;

use App\Extends\Services\Resume\ElasticService;
use App\Models\Resume\PersonalResume;
use App\Models\TagRelate\SkillTagRelate;
use Illuminate\Support\Facades\DB;

class HomeController extends Front
{
    public function index()
    {

        $keyword = trim($this->request->get("keyword"));
        $seachWord = $keyword;
        // 先查出标签关联
        if (trim($keyword)) {
            $list = DB::table(SkillTagRelate::tableName())->where('tag_name', 'like', "%$keyword%")->select(["*"])->get();
            $tagArr = array($keyword);
            foreach ($list as $item) {
                array_push($tagArr, $item->tag_relate);
            }
            $seachWord = join(' ', $tagArr);
        }

        $elastic = new ElasticService();
        $result = $elastic->search($seachWord);

        $list = array();
        if (array_key_exists('hits', $result)) {
            $hits = $result['hits'];
            $total = $hits['total'];
            if ($total > 0) {
                $list = $hits['hits'];
            }
        }

//        echo json_encode($result, JSON_UNESCAPED_UNICODE);

        $data = ['msg' => 'index--ok', 'code' => 200, 'keyword' => $keyword, 'list' => $list];
        return $this->render('', $data);
    }

}